package com.example.springboot_ruiji.controller;

import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.pojo.Employee;
import com.example.springboot_ruiji.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        R<Employee> r=employeeService.login(employee);
        if (r.getCode()==1){
            session.setAttribute("employee",r.getData().getId());
        }
        return employeeService.login(employee);
    }
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,@RequestParam(required = false) String name){
        pageSize=10;
        return employeeService.page(page,pageSize,name);
    }
    @PostMapping
    public R add(@RequestBody Employee employee){
        return employeeService.add(employee);
    }
}
